﻿ #region Disclaimer/Info
 
 /////////////////////////////////////////////////////////////////////////////////////////////////
 //
 //   File:		EmailMessage.Builder.cs
 //   Website:		http://dexterblogengine.com/
 //   Authors:		http://dexterblogengine.com/About.ashx
 //   Rev:		1
 //   Created:		19/01/2011
 //   Last edit:		19/01/2011
 //   License:		GNU Library General Public License (LGPL)
 //   File:            EmailMessage.Builder.cs
 //   For updated news and information please visit http://dexterblogengine.com/
 //   Dexter is hosted to Codeplex at http://dexterblogengine.codeplex.com
 //   For any question contact info@dexterblogengine.com
 //
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 
 #endregion
 
using System;
using System.Net.Mail;
using Topics.Radical.Validation;

namespace Dexter.Domain.Model {
	public partial class EmailMessage {
		/// <summary>
		/// Creates a new instance of <see cref="EmailMessage"/>.
		/// </summary>
		/// <param name="from">The sender.</param>
		/// <param name="to">The receiver.</param>
		/// <param name="subject">The subject</param>
		/// <param name="body">The body (html formatted)</param>
		/// <returns>A new instance of <see cref="EmailMessage"/></returns>
		/// <exception cref="ArgumentNullException">If <paramref name="from"/> is <c>null</c>.</exception>
		/// <exception cref="ArgumentNullException">If <paramref name="to"/> is <c>null</c>.</exception>
		/// <exception cref="ArgumentNullException">If <paramref name="subject"/> is <c>null</c> or <c>empty</c>.</exception>
		/// <exception cref="ArgumentNullException">If <paramref name="body"/> is <c>null</c> or <c>empty</c>.</exception>
		public static EmailMessage CreateNewEmailMessage ( MailAddress from , MailAddress to , string subject , string body ) {
			Ensure.That(from)
				.Named("from")
				.WithPreview((v, e) => Logger.Warn(v.GetFullErrorMessage(), e))
				.IsNotNull()
				.WithMessage("Invalid from.");

			Ensure.That(to)
				.Named("to")
				.WithPreview((v, e) => Logger.Warn(v.GetFullErrorMessage(), e))
				.IsNotNull()
				.WithMessage("Invalid to.");

			Ensure.That(subject)
				.Named("subject")
				.WithPreview((v, e) => Logger.Warn(v.GetFullErrorMessage(), e))
				.IsNotNullNorEmpty (  )
				.WithMessage("Invalid subject.");

			Ensure.That(body)
				.Named("body")
				.WithPreview((v, e) => Logger.Warn(v.GetFullErrorMessage(), e))
				.IsNotNullNorEmpty()
				.WithMessage("Invalid body.");
			
			return new EmailMessage ( from , to , subject , body );
		}
	}
}
